/**
 * 公共
 */

// 处理like

// 字符串处理
if (!String.prototype.trim) {
    String.prototype.trim = function () {
        return this.replace(/(^\s*)|(\s*$)/g, '');
    }
}


//消息全部标记为已读
$('.mark-all').on('click',function(){
    $.ajax({
        url: page.msg_url,
        type: 'POST',
        dataType: 'json',
        success: function(obj){
            if(obj.code === 0){
                $('.msg-list li').remove();
                $('.top-msg-list li').remove();
                $('#msgNum').remove();
                $('.msg-list').remove();
                eui.msg('标记成功');
            }
            else{
                eui.alert('信息全部为已读状态，'+obj.msg);
            }
        },
        error: function(){
            eui.alert('系统错误');
        }
    })
})

// 微信二维码
$('#show-weixin').popover({
    html: true,
    placement: 'top',
    content: function () {
        return $('#weixin-box').html();
    }
});

$('#show-weixin').on('mouseover', function () {
    eui.tips('我是另外一个tips，只不过我长得跟之前那位稍有些不一样。', '#show-weixin', {
        tips: [1, '#3595CC'],
        time: 4000,
    });
});


// /**
//  * eui-page
//  * 分页插件
//  * https://github.com/cjhgit/eui-page
//  */
;(function ($) {

    // 分页类
    function Page(elem, option) {
        var that = this;
        that.$elem = $(elem);

        that.opts = $.extend({}, Page.DEFAULT, option);
        that.init();
        that.view();
        that.event();
    }

    Page.DEFAULT = {
        page: 1, // 当前页数
        pageSize: 10, // 每页显示条数
        totalPage: 1, // 总页数
        prevPage: false, // 是否显示上一页
        nextPage: false, // 是否显示下一页,
        count: 5, // 显示页数数量
        onPage: function (page) {}
    };

    Page.pt = Page.prototype;

    Page.pt.init = function () {
        var that = this;
        that.page = that.opts.page;
        that.totalPage = that.opts.totalPage;
    };

    Page.pt.view = function () {
        var that = this;

        that.$elem.addClass('tc-page-list');
        that.$elem.empty();
        $ul = that.$elem;

        var halfCount = parseInt(that.opts.count / 2);
        var startPage =  that.page - halfCount;
        if (startPage < 1) {
            startPage = 1;
        }
        if (that.totalPage > that.opts.count
            && startPage > (that.totalPage - that.opts.count + 1)) {
                startPage = that.totalPage - that.opts.count + 1;
        }

        var endPage = startPage + that.opts.count - 1;
        if (endPage > that.totalPage) {
            endPage = that.totalPage;
        }
        // 上一页
        if (that.opts.prevPage && that.page > 1) {
            var li = '<li class="page-item"><a class="page-link" href="#" data-page="' + (that.page - 1) + '"><span>&laquo;</span></a></li>';
            $ul.append($(li));
        }
        // 页数
        for (var i = startPage; i <= endPage; i++) {
            var active = i === that.page ? ' active' : '';
            var li = '<li class="page-item' + active + '"><a class="page-link" href="#" data-page="' + i + '">' + i + '</a></li>';
            $ul.append($(li));
        }
        // 下一页
        if (that.opts.nextPage && that.page < that.totalPage) {
            var li = '<li class="page-item"><a class="page-link" href="#" data-page="' + (that.page + 1) + '"><span>&raquo;</span></a></li>';
            $ul.append($(li));
        }
    };

    Page.pt.event = function () {
        var that = this;
        that.$elem.on('click', '[data-page]', function (e) {
            e.preventDefault(); // TODO options
            var page = $(this).data('page');
            that.page = page;
            that.view();
            typeof that.opts.onPage == 'function' && that.opts.onPage(page);
        });
    };

    $.fn.page = function (option) {
        return $(this).each(function () {
            new Page(this, option);
        });
    };

    window.eui = window.eui || {};
    window.eui.Page = Page;
})(jQuery);

// 加载更多插件
;(function ($) {

    // 分页类
    function Loadmore(elem, option) {
        var that = this;
        that.$elem = $(elem);

        that.opts = $.extend({}, Loadmore.DEFAULT, option);
        that.init();
        that.event();
    }

    Loadmore.DEFAULT = {
        page: 1, // 当前页数
        pageSize: 10, // 每页显示条数
        totalPage: 1, // 总页数
        prevPage: false, // 是否显示上一页
        nextPage: false, // 是否显示下一页,
        count: 5, // 显示页数数量
        onPage: function (page) {}
    };

    Loadmore.pt = Loadmore.prototype;

    Loadmore.pt.init = function () {
        var that = this;
        that.page = that.opts.page;
        that.totalPage = that.opts.totalPage;
    };

    Loadmore.pt.event = function () {
        var that = this;
        that.$elem.on('click', function (e) {
            e.preventDefault();
            if (that.page === that.totalPage) {
                eui.msg('没有下一页了');
                return;
            }
            that.page += 1;
            console.log(that.page, that.totalPage);
            if (that.page === that.totalPage) {
                that.$elem.hide();
            }
            typeof that.opts.onPage == 'function' && that.opts.onPage(that.page);
        });
    };

    $.fn.loadmore = function (option) {
        return $(this).each(function () {
            new Loadmore(this, option);
        });
    };

    window.eui = window.eui || {};
    window.eui.Loadmore = Loadmore;
})(jQuery);

// 修改插件默认配置
$.extend(eui.Page.DEFAULT, {
    nextPage: true,
    prevPage: false
});

// 修改默认遮罩层样式
$.extend(eui.overlay.DEFAULT, {
    bgColor: '#000',
    opacity: 0.4,
    zIndex: 100
});

// 修改对话框默认配置
$.extend(eui.Dialog.DEFAULTS, {
    closeBtn: false,
    shadeClose: true,
    //type: 1,
    title: false,
    shade: [0.4, '#000'],
    draggable: false,
    closeBtn: false
});
$.extend(eui.Alert.DEFAULTS, {
    title: false,
    closeBtn: false,
});
$.extend(eui.Prompt.DEFAULTS, {
    closeBtn: false,
});
window.console = window.console || (function(){
    var c = {}; c.log = c.warn = c.debug = c.info = c.error = c.time = c.dir = c.profile
        = c.clear = c.exception = c.trace = c.assert = function(){};
    return c;
})();

/* 学生登陆框 */
$('#login-box-login').on('click', function () {
    var email = $('#login-box-email').val();
    if (!email) {
        eui.msg('邮箱不能为空');
        return;
    }
    var pwd = $('#login-box-password').val();
    if (!pwd) {
        eui.msg('密码不能为空');
        return;
    }

    $.ajax({
        url: '/user/login',
        data: {
            user: email,
            password: pwd
        },
        type: 'POST',
        dataType: 'json',
        success: function (obj) {
            if (obj.code === 0) {
                window.location.reload(true);
            } else {
                eui.msg('登陆失败，' + obj.msg);
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            eui.alert('系统出错');
        }
    });
});

/* 企业登陆框 */
$('#login-box-login2').on('click', function () {
    var email = $('#login-box-email2').val();
    if (!email) {
        eui.msg('邮箱不能为空');
        return;
    }
    var pwd = $('#login-box-password2').val();
    if (!pwd) {
        eui.msg('密码不能为空');
        return;
    }

    $.ajax({
        url: '/company/user/login',
        data: {
            user: email,
            password: pwd
        },
        type: 'POST',
        dataType: 'json',
        success: function (obj) {
            if (obj.code === 0) {
                window.location.reload(true);
            } else {
                eui.msg('登陆失败，' + obj.msg);
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            eui.alert('系统出错');
            //alert(XMLHttpRequest.status);
            //alert(XMLHttpRequest.readyState);
            //alert(textStatus);
        }
    });
});

// 退出
$('#tc-logout').on('click', function (e) {
    e.preventDefault();

    $.ajax({
        url: '/user/logout',
        //type: 'POST',
        dataType: 'json',
        success: function (obj) {
            if (obj.code === 0) {
                window.location.reload(true);
            } else {
                eui.msg('退出失败，' + obj.msg);
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            eui.msg('系统出错');
        }
    });
});

/* 返回 */
$('.layout-header .header-back').on('click', function (e) {
    e.preventDefault();
    console.log(window.history);

    if (window.history.length > 0) {
        window.history.go(-1);
    } else {
        var href = $(this).attr('href');
        if (href === '#') {
            window.location.href = '/';
        } else {
            window.location.href = href;
        }

    }


})